package com.spring.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.spera.aliyunUtil.CreateOSSClient;

public class SpUtil {
	
	/** 
	 * 删除单个文件 
	 * @param   sPath    被删除文件的文件名 
	 * @return 单个文件删除成功返回true，否则返回false 
	 */  
	public static boolean deleteFile(String sPath) {  
	    boolean flag = false;  
	    File file = new File(sPath);  
	    // 路径为文件且不为空则进行删除  
	    if (file.isFile() && file.exists()) {  
	        file.delete();  
	        flag = true;  
	    }  
	    return flag;  
	} 
	
	
	public static String getSessionId(){
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
	    ServletRequestAttributes sra = (ServletRequestAttributes)ra;  
	    HttpServletRequest request = sra.getRequest();
	    return request.getSession().getId();
	}
	
	/**
	 * 验证Email
	 * 
	 * @param email
	 */
	public static boolean validateEmail(String email) {
		String regex = "^(\\w-*\\.*)+@(\\w-?)+(\\.\\w{2,})+$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}

	/**
	 * 验证手机号
	 * 
	 * @param phoneNum
	 */
	public static boolean validatePhone(String phoneNum) {
		Pattern pattern = Pattern
				.compile("^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|(18[0-9]))\\d{8}$");
		
		
		Matcher matcher = pattern.matcher(phoneNum);
		return matcher.matches();
	}

	/**
	 * chekc正则
	 */
	public static boolean match(String str, String pat) {
		Pattern pattern = Pattern.compile(pat);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	
	
	public static String imageFileUpOss(String bucketName, String key, InputStream fis,Long fileSize)
            throws Exception {
    	OSSClient client = CreateOSSClient.getInstance().getOSSClient();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(fileSize);
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
       
        client.putObject(bucketName, key, fis, objectMeta);
        return "http://" + bucketName + ".img-cn-hangzhou.aliyuncs.com/" + key;
    }
	
    public static String imageFileUpOss(String bucketName, String key, File file)
            throws Exception {
    	FileInputStream fis = new FileInputStream(file);
    	OSSClient client = CreateOSSClient.getInstance().getOSSClient();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(file.length());
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
       
        client.putObject(bucketName, key, fis, objectMeta);
        return "http://" + bucketName + ".img-cn-hangzhou.aliyuncs.com/" + key;
    }
    
    public static ImputStreamObj getHttpInputStream(String netUrl) throws Exception{
    	URL url = new URL(netUrl);
		HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();  
		httpUrl.connect(); 
		ImputStreamObj obj = new ImputStreamObj(httpUrl.getInputStream(),Long.parseLong(httpUrl.getHeaderField("Content-Length")));
		return obj;
    }

	public static String fileUpOss(String bucketName, String key, InputStream fis, String contentType,Long fileSize) throws Exception {
		OSSClient client = CreateOSSClient.getInstance().getOSSClient();
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.setContentLength(fileSize.longValue());
		objectMeta.setContentType(contentType);
		client.putObject(bucketName, key, fis, objectMeta);
		return "http://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/" + key;
	}

}
